package com.base.common.webservice.util;

import com.base.common.json.JsonUtil;
import com.base.common.utils.StringUtils;
import com.base.common.webservice.HZSYS86Service.CstService;
import com.base.common.webservice.HZSYS86Service.CstServiceSoap;
import com.base.common.webservice.SYS86Service.SYS86Service;
import com.base.common.webservice.SYS86Service.SYS86ServiceSoap;
import com.base.common.webservice.ServiceConfigConstants;
import com.base.common.webservice.entity.SYS86Service.*;
import com.base.common.webservice.enums.FeeType;
import com.base.common.webservice.factory.HZServiceFactory;
import com.base.common.webservice.factory.SYS86ServiceFactory;
import com.base.common.webservice.factory.WebServiceFactory;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringEscapeUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author Kat Colin
 *
 */
public class SYS86ServiceUtil {

	private static String strStoken = ServiceConfigConstants.STRSTOKEN;

	/**
	 * 访问接口获得返回数据
	 * 
	 * @param clazz
	 *            接收数据实体类类型
	 * @param sys86ServiceArgs
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> getSYS86Service(Class<?> clazz,
			SYS86ServiceArgs sys86ServiceArgs) {
		// 创建webservice工厂
		WebServiceFactory webServiceFactory = SYS86ServiceFactory.getInstance();

		// 获取service
		SYS86Service sys86Service = ((SYS86ServiceFactory)webServiceFactory).getSYS86Service();
		SYS86ServiceSoap sys86ServiceSoap = sys86Service.getSYS86ServiceSoap();

		/*System.out.println(sys86ServiceArgs.getP0() + "/n" + sys86ServiceArgs.getP1() +
				"/n" + sys86ServiceArgs.getP2() + "/n" + sys86ServiceArgs.getP3()
				+ "/n" + sys86ServiceArgs.getP4() +
				"/n" + sys86ServiceArgs.getP5()
				+"/n" + sys86ServiceArgs.getP7());*/
		// 访问webservice，获取数据
		List<Object> objs = (List<Object>) JsonUtil.string2Obejct(
				sys86ServiceSoap.getSYS86Service(
						sys86ServiceArgs.getStrToken(),
						sys86ServiceArgs.getP0(), sys86ServiceArgs.getP1(),
						sys86ServiceArgs.getP2(), sys86ServiceArgs.getP3(),
						sys86ServiceArgs.getP4(), sys86ServiceArgs.getP5(),
						sys86ServiceArgs.getP6(), sys86ServiceArgs.getP7()),
				Map.class).get(sys86ServiceArgs.getP0());
		Map<String, Object> map = (Map<String, Object>) objs.get(0);

		List<T> ts = new ArrayList<T>();
		for (Map<String, Object> object : (List<Map<String, Object>>) map
				.get("Syswin")) {
			T t = EasyUtils.mapToObject(clazz, object);
			ts.add(t);
		}
		return ts;
	}

	/**
	 * 按客户获取该客户所有资源应缴费用
	 * 
	 * @param CusID
	 *            客户ID
	 * @param startTime
	 *            开始年月， 可为空
	 * @param endTime
	 *            结束年月，可为空
	 * @param ProjectID
	 *            项目ID
	 * @param feeType
	 *            查询费用范围，，可为空，为空则查询所有费用 {@linkplain FeeType}
	 * @return
	 */
	public static List<UserRevGetFeeListBean> getUserRev_GetFeeList(
			String CusID, String startTime, String endTime, String ProjectID,
			String feeType) {
		SYS86ServiceArgs sys86ServiceArgs = new SYS86ServiceArgs();
		sys86ServiceArgs.setStrToken(strStoken);
		sys86ServiceArgs.setP0(ServiceConfigConstants.USERREV_GETFEELIST);
		sys86ServiceArgs.setP1(CusID);
		sys86ServiceArgs.setP2(startTime);
		sys86ServiceArgs.setP3(endTime);
		sys86ServiceArgs.setP4(ProjectID);
		sys86ServiceArgs.setP5(feeType);

		// xxxxUtil调用接口方法

		List<UserRevGetFeeListBean> feeListBeans = SYS86ServiceUtil
				.getSYS86Service(UserRevGetFeeListBean.class, sys86ServiceArgs);
		return feeListBeans;
	}

	/**
	 * 按资源获取该资源所有应缴费用
	 * 
	 * @param ResID
	 *            资源ID
	 * @param startTime
	 *            开始年月，可为空
	 * @param endTime
	 *            结束年月，可为空
	 * @param ProjectID
	 *            项目ID
	 * @param feeType
	 *            查询费用范围 ，可为空，为空则查询所有费用{@linkplain FeeType}
	 * @return
	 */
	public static List<UserRevGetFeeListByResBean> getUserRev_GetFeeListByRes(
			String ResID, String startTime, String endTime, String ProjectID,
			String feeType) {
		SYS86ServiceArgs sys86ServiceArgs = new SYS86ServiceArgs();
		sys86ServiceArgs.setStrToken(strStoken);
		sys86ServiceArgs.setP0(ServiceConfigConstants.USERREV_GETFEELISTBYRES);
		sys86ServiceArgs.setP1(ResID);
		sys86ServiceArgs.setP2(startTime);
		sys86ServiceArgs.setP3(endTime);
		sys86ServiceArgs.setP4(ProjectID);
		sys86ServiceArgs.setP5(feeType);

		// xxxxUtil调用接口方法

		List<UserRevGetFeeListByResBean> feeListByResBeans = SYS86ServiceUtil
				.getSYS86Service(UserRevGetFeeListByResBean.class,
						sys86ServiceArgs);
		return feeListByResBeans;
	}

	/**
	 * 根据物业系统中业主或租户姓名检索业主或租户相关信息进行认证
	 * 
	 * @param CusName
	 *            客户姓名
	 * @param ProjectID
	 *            项目ID， 可为空，为空时表示所有项目
	 * @return
	 */
	public static List<UserRevGetOwnerInforBean> getUserRev_GetOwnerInfor(
			String CusName, String ProjectID) {

		// 配置参数

		SYS86ServiceArgs sys86ServiceArgs = new SYS86ServiceArgs();
		sys86ServiceArgs.setStrToken(strStoken);
		sys86ServiceArgs.setP0(ServiceConfigConstants.USERREV_GETOWNERINFOR);
		sys86ServiceArgs.setP1(CusName);
		sys86ServiceArgs.setP2(ProjectID);

		// xxxxUtil调用接口方法

		List<UserRevGetOwnerInforBean> ownerInforBeans = SYS86ServiceUtil
				.getSYS86Service(UserRevGetOwnerInforBean.class,
						sys86ServiceArgs);
		return ownerInforBeans;
	}

	/**
	 * 根据物业系统中业主、租户姓名和预留的手机号进行认证
	 * 
	 * @param CusName
	 *            客户名称，可为空
	 * @param MobileNo
	 *            客户手机号
	 * @param ProjectID
	 *            项目ID， 可为空，为空时表示所有项目
	 * @return
	 */
	public static List<UserRevOwnerVerifyBean> getUserRev_OwnerVerify(
			String CusName, String MobileNo, String ProjectID) {
		// 配置参数

		SYS86ServiceArgs sys86ServiceArgs = new SYS86ServiceArgs();
		sys86ServiceArgs.setStrToken(strStoken);
		sys86ServiceArgs.setP0(ServiceConfigConstants.USERREV_OWNERVERIFY);
		sys86ServiceArgs.setP1(CusName);
		sys86ServiceArgs.setP2(MobileNo);
		sys86ServiceArgs.setP3(ProjectID);

		// xxxxUtil调用接口方法

		List<UserRevOwnerVerifyBean> revOwnerVerifyBeans = SYS86ServiceUtil
				.getSYS86Service(UserRevOwnerVerifyBean.class, sys86ServiceArgs);
		return revOwnerVerifyBeans;
	}

	/**
	 * 按应收款进行缴费
	 * 
	 * @param CusID
	 * @param ProjectID
	 * @param terminalName
	 * @param dateSource
	 * @param payWay
	 * @param payFeeInfo
	 * @return
	 */
	public static UserRevPayFeeBean UserRev_PayFee(String CusID, String ProjectID, String terminalName, String dateSource, String payWay, String payFeeInfo) {
		SYS86ServiceArgs sys86ServiceArgs = new SYS86ServiceArgs();
		sys86ServiceArgs.setStrToken(strStoken);
		sys86ServiceArgs.setP0(ServiceConfigConstants.USERREV_PAYFEE);
		sys86ServiceArgs.setP1(CusID);
		sys86ServiceArgs.setP2(ProjectID);
		sys86ServiceArgs.setP3(terminalName);
		sys86ServiceArgs.setP4(dateSource);
		sys86ServiceArgs.setP5(payWay);
		sys86ServiceArgs.setP7(payFeeInfo);
		// xxxxUtil调用接口方法

		List<UserRevPayFeeBean> revOwnerVerifyBeans = SYS86ServiceUtil
				.getSYS86Service(UserRevPayFeeBean.class, sys86ServiceArgs);
		return revOwnerVerifyBeans.get(0);
	}

	public static void main(String[] args) {
		// 创建webservice工厂
		WebServiceFactory webServiceFactory = HZServiceFactory.getInstance();
		HZServiceFactory hzServiceFactory = (HZServiceFactory) webServiceFactory;

		// 获取service
		CstService cstService = hzServiceFactory.getCstService();
		CstServiceSoap cstServiceSoap = cstService.getCstServiceSoap();
		Map m = new LinkedHashMap();
		m.put("RevID", "20170510161049910003");
		m.put("RevMoney", 0.01);
		m.put("LFMoney", 0);
		m.put("TradingID", "123456");
		m.put("Trading", "微信");
		m.put("Filldate", "2017-05-18");
		m.put("RBank", "");
		m.put("RAccount", "");
		m.put("OrgID", "10000");
		List list = new ArrayList<>();
		list.add(m);
		Map p1 = new HashedMap();
		p1.put("Syswin", list);
		System.out.println(JsonUtil.dto2String(p1));
		System.out.println(cstServiceSoap.getETSService("SSSySWIN*(SK_WH()", ServiceConfigConstants.HZ_USERREV_PAYFEEBYBANK, JsonUtil.dto2String(p1), "", "", "", "", "", ""));
		/*List<Object> objects  = (List<Object>) (JsonUtil.string2Obejct(cstServiceSoap.getService(ServiceConfigConstants.HZ_USERREV_PAYFEEBYBANK, "17051016060800020064", "", "", "", "", "", ""), Map.class).get("UserRev_GetFeeListbyLocklogo"));
		Map<String, Object> map = (Map<String, Object>) objects.get(0);
//        List<UserCstGetUsersByMobilePhoneBean> ts = new ArrayList<UserCstGetUsersByMobilePhoneBean>();
        for (Map<String, Object> object : (List<Map<String, Object>>) map
                .get("Syswin")) {
//            System.out.println(object.get("CstID").toString());
            System.out.println(JsonUtil.dto2String(object));
//            UserCstGetUsersByMobilePhoneBean t = EasyUtils.mapToObject(UserCstGetUsersByMobilePhoneBean.class, object);
//            t.setCstName(StringEscapeUtils.unescapeJava(t.getCstName()));
//            ts.add(t);
        }*/

    }

}
